<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>飞机票列表</title>
    <link rel="stylesheet" href="../../../script/swiper/swiper.min.css">
    <link rel="stylesheet" href="../../../css/index/aircraft/ticket_list.css">
    <link rel="stylesheet" href="../../../css/index/hotel/date.css">
    <link rel="stylesheet" href="../../../script/widget/slider/jquery.range.css">
    <link rel="stylesheet" href="../../../css/aui.css">
    <link rel="stylesheet" href="../../../css/iconfont/iconfont.css">
</head>
<body style="cursor: pointer;">
<!-- 日期选择 -->
<div class="swiper-container" style="height:3.5rem;">
    <div class="swiper-wrapper" id="calendar">

    </div>
</div>
<div class="swiper-slide" id="date-time" style="height:3.5rem;line-height: 3.5rem;border-left:1px solid #59BFDD;width: 16.6%;">
    <i class="iconfont icon-rili1" style="font-size: 1.2rem;"></i>
    <p style="color:#ffffff;margin-top:-2rem;">日历</p>
</div>

<!--列表内容-->
<div class="aui-content">
    <div class="aui-list aircraft-list" id="aircraft_lists">


    </div>
</div>
</body>
</html>
<script src="../../../script/extend/jquery-3.3.1.min.js"></script>
<script src="../../../script/aui/api.js"></script>
<script src="../../../script/aui/aui-tab.js"></script>
<script src="../../../script/swiper/swiper.min.js"></script>
<script src="../../../script/widget/slider/jquery.range.js"></script>
<!-- <script src="../../../script/aui/aui-dialog.js"></script> -->
<script src="../../../script/aui/aui-toast.js"></script>
<script src="../../../script/extend/date.js"></script>
<script src="../../../script/config.js"></script>
<script type="text/javascript" src="../../../script/common.js"></script>
<script src="../../../script/extend/doT.min.js"></script>
<script src="../../../script/widget/js-time.js"></script>

<!--飞机票列表 start-->
<script id="aircrafttmp" type="text/x-dot-template">
{{ for(var prop in it) { }}
<div class="aui-list-item aui-list-item-inner" style="margin-bottom: 0.5rem;" onclick="call_photo(this)">
    <div class="aui-list-item-title">
        <div class="aui-hide plane_number">{{=it[prop]['flight_number']}}</div>
        <div class="aui-hide off_code">{{=it[prop]['take_off_code']}}</div>
        <div class="aui-hide ground_code">{{=it[prop]['to_ground_code']}}</div>
        <div class="aui-hide airline_code">{{=it[prop]['airline_code']}}</div>
        <div class="aui-hide airline_model">{{=it[prop]['airline_model']}}</div>
        <div class="aui-hide flight_hours">{{=it[prop]['flight_hours']}}</div>
        <div class="aui-hide flight_minute">{{=it[prop]['flight_minute']}}</div>
        <div class="aui-hide take_off_floor">{{=it[prop]['take_off_floor']}}</div>
        <div class="aui-hide to_ground_floor">{{=it[prop]['to_ground_floor']}}</div>
        <div class="aui-hide take_off_place">{{=it[prop]['take_off_place']}}</div>
        <div class="aui-hide to_ground_place">{{=it[prop]['to_ground_place']}}</div>

        <span class="start_time">{{=it[prop]['take_off_time']}}</span>
        <span class="end_time">{{=it[prop]['to_ground_time']}}</span>
        <span class="start_adder">{{=it[prop]['take_off_place_full']}}</span>
        <span class="information_to">{{=it[prop]['airline_name']}}{{=it[prop]['flight_number']}}</span>
        <span class="end_adder">{{=it[prop]['to_ground_place_full']}}</span>
        <!--<span class="information-from">{{=aircraf_end_site}}</span>-->
        {{if(it[prop]['flight_minute']){ }}
        <span class="duration">{{=it[prop]['flight_hours']}}小时{{=it[prop]['flight_minute']}}分</span>
        {{ }else{ }}
        <span class="duration">{{=it[prop]['flight_hours']}}小时00分</span>
        {{ } }}
        <i class="iconfont icon-lianjie"></i>
    </div>
    <div class="aui-list-item-right">
        <p><e>￥</e>{{=it[prop]['price']}}</p>
        <p>
            {{if(it[prop]['discount'] != 0){ }}
            {{=it[prop]['shipping_space']}}
            <hr/>
            {{=it[prop]['discount']}}折
            {{ }else{ }}
            {{=it[prop]['shipping_space']}}
            {{ } }}
        </p>
        <!--<p>时段内最低价</p>-->
    </div>
</div>
{{ } }}
</script>
<!-- 飞机票列表 end -->
<script>
    //出发时间,始发地,目的地
    var travel_time,isApply,select_time,numDays,moveNum;
    apiready = function(){
      api.parseTapmode();
      selected_staff = api.pageParam.selected_staff;
      //初始化日期滑动选择
      aircraf_start_time = $api.getStorage('aircraf_start_time');
      aircraf_start_site = $api.getStorage('aircraf_start_site');
      aircraf_end_site = $api.getStorage('aircraf_end_site');
      isApply = $api.getStorage('isApply');
      calculateTime();

      getNumDay(aircraf_start_time);
      var confirmTime = getBeforeDate(numDays,aircraf_start_time);
      calculate(confirmTime);
      get_aircraft_list();

      //计算天数，日期位移
      function getNumDay (aircraf_start_time) {
        var date = new Date();
        var nowDate = date.getFullYear()+'-'+PrefixInteger((date.getMonth()+1),2)+'-'+PrefixInteger(date.getDate(),2);//获取当前日期
        if (aircraf_start_time.split('/').join("-") == nowDate) {
            numDays = 0;
        } else {
          numDays = parseInt(Math.abs((new Date(aircraf_start_time)).getTime()-date.getTime())/3600/24/1000)+1;//选择的日期(毫秒)
        }
        if (numDays <= 5) {
          numDays = numDays;
          if (numDays <= 3) {
            moveNum = 0;
          } else {
            moveNum = numDays - 2;
          }
        } else {
            numDays = 5;
            moveNum = 3;
        }
      }
      /*打开日期选择*/
      var IsRun = false;
      $('#date-time').hotelDate({date:2,start_date:aircraf_start_time},function(ret){
        if (isApply == 1) {// 1 开启 0 关闭
          //判断选择的时间不能超过申请单时间
          if ($api.getStorage('travel_time').valueOf() > ret.enterTime.valueOf()) {
              toast.fail({
                  title: "出行日期需大于等于出差申请单中的申请日期",
                  duration: 4000
              });
              return false;
          }
        }
          //选中时间
          var checkTime = ret.enterTime;
          getNumDay(checkTime);
          var confirmTime = getBeforeDate(numDays,checkTime);
          aircraf_start_time = confirmTime;
          var plane_start_time = $api.setStorage('aircraf_start_time',checkTime);
          select_time = checkTime;
          get_aircraft_list();
          if(IsRun) calculate(confirmTime);
          IsRun = true;
      });
    }
    /**
     * 滑动日期选择点击
     * dateNum 周几
     * year 年
     * month 月
     * date 日
     * obj 本对象
     **/
    function dateClick(dateNum,year,month,date,obj) {
      if (isApply == 1) {// 1 开启 0 关闭
        select_time = year+'/'+PrefixInteger(month,2)+'/'+PrefixInteger(date,2);
        //判断选择的时间不能超过申请单时间
        console.log($api.getStorage('travel_time'));
        if ($api.getStorage('travel_time').valueOf() > select_time.valueOf()) {
            toast.fail({
                title: "出行日期需大于等于出差申请单中的申请日期",
                duration: 4000
            });
            return false;
        }
      }

        $(obj).parent().find('.swiper-slide').removeClass('tomorrow');
        $(obj).addClass('tomorrow');
        $api.setStorage('aircraf_start_time',year+'/'+month+'/'+date);
        get_aircraft_list();
    };
    //计算日期 平移效果
    function calculate(Ymd) {
        var width_num = $('.swiper-slide').eq(0).width();
        var distance = 0;
        distance = -width_num*moveNum;
        $('#calendar').css({'transform':'translate3d('+distance+'px, 0px, 0px)','transition-duration':'800ms'});
        calculateTime(Ymd);
    }
    //num传入的数字，n需要的字符长度  比如：2018-9-9  转换成 2018-09-09
        function PrefixInteger(num, n) {
            return (Array(n).join(0) + num).slice(-n);
        }

    /**
     *推算当天之后指定天数日历
     * @param assignDate 指定日期
     */
    function calculateTime(assignDate) {
      if(assignDate == undefined){
          var now = new Date();
          var sign = false;
          assignDate = now.getFullYear()+'-'+now.getMonth()+'-'+now.getDay();
      }else{
          var now = new Date(Date.parse(assignDate));
          var sign = true;
      }
      var numDay = 15;
      var strtime;
      var htmlTime = " ";
      var paramDate = {};
      paramDate.url = Config.plane.aircraftPriceList;
      paramDate.data = {from: aircraf_start_site, arrive: aircraf_end_site, search_day: assignDate};
      ajax(paramDate,function(data){
        if (data.code == 1) {
          if(!sign){
              for (var i=0; i< numDay; i++) {
                  strtime = addDate(now,i);
                  var strMonth = getMonth(strtime).toString().length > 1 ? getMonth(strtime) : '0'+getMonth(strtime);
                  var strday = getDate(strtime).toString().length > 1 ? getDate(strtime) : '0'+getDate(strtime);
                  if(i == 1) {
                      htmlTime += '<div class="swiper-slide tomorrow date-list" onclick="dateClick('+i+','+getYear(strtime)+','+getMonth(strtime)+','+getDate(strtime)+',this)"><e>';
                      htmlTime += getDay(strtime);
                      htmlTime += '</e><c>';
                      htmlTime += strMonth + '-' + strday;
                      htmlTime += '</c><f>';
                      htmlTime += '￥'+data.data[i].price;
                      htmlTime += '</f></div>';
                  }else{
                      htmlTime += '<div class="swiper-slide date-list" onclick="dateClick('+i+','+getYear(strtime)+','+getMonth(strtime)+','+getDate(strtime)+',this)"><e>';
                      htmlTime += getDay(strtime);
                      htmlTime += '</e><c>';
                      htmlTime += strMonth + '-' + strday;
                      htmlTime += '</c><f>';
                      htmlTime += '￥'+data.data[i].price;
                      htmlTime += '</f></div>';
                  }
              }
          }else{
              for (var i=0; i< numDay; i++) {
                  strtime = addDate(now,i);
                  var strMonth = getMonth(strtime).toString().length > 1 ? getMonth(strtime) : '0'+getMonth(strtime);
                  var strday = getDate(strtime).toString().length > 1 ? getDate(strtime) : '0'+getDate(strtime);
                  if(i == numDays) {
                      htmlTime += '<div class="swiper-slide tomorrow date-list" onclick="dateClick('+i+','+getYear(strtime)+','+getMonth(strtime)+','+getDate(strtime)+',this)"><e>';
                      htmlTime += getDay(strtime);
                      htmlTime += '</e><c>';
                      htmlTime += strMonth+'-'+strday;
                      htmlTime += '</c><f>';
                      htmlTime += '￥'+data.data[i].price;
                      htmlTime += '</f></div>';
                  }else{
                      htmlTime += '<div class="swiper-slide date-list" onclick="dateClick('+i+','+getYear(strtime)+','+getMonth(strtime)+','+getDate(strtime)+',this)"><e>';
                      htmlTime += getDay(strtime);
                      htmlTime += '</e><c>';
                      htmlTime += strMonth+'-'+strday;
                      htmlTime += '</c><f>';
                      htmlTime += '￥'+data.data[i].price;
                      htmlTime += '</f></div>';
                  }
              }
          }
          $('#calendar .date-list').remove();
          $('#calendar').prepend(htmlTime);
          /*日期轮播*/
          var swiper = new Swiper('.swiper-container', {
              slidesPerView: 5,
              spaceBetween: 0,
              freeMode: true,
              pagination: {
                  el: '.swiper-pagination'
              }
          });
        }
      })
    }

    // var dialog = new auiDialog();
    var toast = new auiToast();
    /*请求飞机票列表接口*/
    function get_aircraft_list () {
        aircraf_start_time = $api.getStorage('aircraf_start_time');//起飞日期
        var aircraf_start_site = $api.getStorage('aircraf_start_site');//出发站
        var aircraf_end_site = $api.getStorage('aircraf_end_site');//到达站
        var param = {};
        param.url = Config.plane.aircraftLists;
        param.data = {start_place: aircraf_start_site, end_place: aircraf_end_site, date: aircraf_start_time};
        $('#aircraft_lists').html('');
        $('body').css('background','#ffffff');
        ajax(param,function(ret){
            $('body').css('background','#78DDFF');
            if(ret.code == 1){
                var evalText = doT.template($("#aircrafttmp").text());
                $("#aircraft_lists").html(evalText(ret.data));
            } else {
                $("#aircraft_lists").text('');
                $toast('没有航班信息',2000,'m');

            }
        })
    }

    //选择航班的信息
    call_photo = function(obj) {
        travel_time = $api.getStorage('travel_time');
        if (isApply == 1 && $api.getStorage('order_self') != 1 && new Date(select_time).getTime() < new Date(travel_time).getTime()) {
          toast.fail({
              title: "出行日期需大于等于出差申请单中的申请日期",
              duration: 4000
          });
          return false;
        }

        var flight_number = $(obj).find('.plane_number').text();
        var airline_code  = $(obj).find('.airline_code').text();
        var date          = $api.getStorage('aircraf_start_time');
        var time          = $(obj).find('.start_time').text();
        var planeArray = {
          'flight_number'    : flight_number,//航班号
          'date'             : date,//出发日期
          'time'             : time,//起飞时间
          'end_time'         : $(obj).find('.end_time').text(),//结束时间
          'carrier'          : airline_code,//航空公司两字码
          'information_to'   : $(obj).find('.information_to').text(),//东航MU5294
          'start_adder'      : $(obj).find('.start_adder').text(),//武宿T2
          'end_adder'        : $(obj).find('.end_adder').text(),//北京T2
          'duration'         : $(obj).find('.duration').text(),//时长
          'airline_model'    : $(obj).find('.airline_model').text(),//机型
          'flight_hours'     : $(obj).find('.flight_hours').text(),//时长（小时）
          'flight_minute'    : $(obj).find('.flight_minute').text(),//时长（分钟）
          'take_off_floor'   : $(obj).find('.take_off_floor').text(),//起飞航站楼
          'to_ground_floor'  : $(obj).find('.to_ground_floor').text(),//到达航站楼
          'take_off_place'   : $(obj).find('.take_off_place').text(),//起飞机场名
          'to_ground_place'  : $(obj).find('.to_ground_place').text(),//到达机场名
          'off_code'         : $(obj).find('.off_code').text(),//出发城市代码
          'ground_code'      : $(obj).find('.ground_code').text(),//到达城市代码
          'week'             : $('.tomorrow e').text()//周一
        }
        api.openWin({
            name: 'plane_info_win',
            url: './plane_info_win.html',
            pageParam: {
                plane:planeArray,
                selected_staff:selected_staff
            }
        });

    }
</script>
